package light.head.controller.el;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import light.head.constant.Msg;
import light.head.constant.Parm;
import light.head.constant.Url;
import light.head.dao.el.TopicDifficultyDAO;
import light.head.dao.sys.OrganDAO;
import light.head.enm.RESULT;
import light.head.framework.base.SupporterController;
import light.head.framework.entity.PageBean;
import light.head.model.el.TopicDifficulty;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping(Url.C_TOPICDIFFICULTY)
public class TopicDifficultyController extends SupporterController {
	/**
	 * 题目难度管理
	 */
	private TopicDifficultyDAO tdDAO;
	private OrganDAO organDAO;

	@RequestMapping(Url.INIT)
	public ModelAndView init(HttpServletRequest request, HttpServletResponse response){
		return new ModelAndView(Url.TOPICDIFFICULTY_INIT);
	}
	
	@RequestMapping(Url.QUERY)
	public ModelAndView query(HttpServletRequest request, HttpServletResponse response){
		PageBean pb = createPageBean(request);
		Map<String, String> map = createQueryMapByParmName(request, Parm.NAME, Parm.DELETED);
		Integer oc = organDAO.getRoot(cu_organCode(request)).getCode();
		tdDAO.findByMap(oc, map, pb);
		request.setAttribute(Parm.PAGE_BEAN, pb);
		return new ModelAndView(Url.TOPICDIFFICULTY_LIST);
	}
	
	@RequestMapping(Url.EDIT)
	public ModelAndView edit(HttpServletRequest request, HttpServletResponse response){
		String code = request.getParameter(Parm.CODE);
		if(isNotNull(code)){
			TopicDifficulty td = tdDAO.get(Integer.parseInt(code));
			request.setAttribute(Parm.LIST, td);
		}
		return new ModelAndView(Url.TOPICDIFFICULTY_EDIT);
	}

	@RequestMapping(Url.SAVE)
	public void save(TopicDifficulty td, HttpServletRequest request, HttpServletResponse response){
		RESULT result = RESULT.ERROR;
		Integer oc = organDAO.getRoot(cu_organCode(request)).getCode();
		if(td.getCode() == null){
			List<TopicDifficulty> list = tdDAO.findByOc(oc, td.getName());
			if (list != null && list.size() != 0){
				print(response, Msg.EXISTS);
				return;
			}
			Date ct = new Date();
			td.setOc(oc);
			td.setCu(cu_code(request));
			td.setCt(ct);
			td.setDeleted(false);
			result = tdDAO.add(td);
		} else {
			List<TopicDifficulty> list = tdDAO.findByOc(oc, td.getName());
			if (list != null && list.size() != 0){
				print(response, Msg.EXISTS);
				return;
			}
			TopicDifficulty tds = tdDAO.get(td.getCode());
			result = tdDAO.upd(tds);
		}
		
		if(RESULT.SUCCESS == result){
			tdDAO.loadAll();	//重新加载难度缓存
			print(response, Msg.SUCCESS);
		}else{
			print(response, Msg.ERROR);
		}
	}
	
	@RequestMapping(Url.SHIELD)
	public void shield(HttpServletRequest request, HttpServletResponse response){
		String code = request.getParameter(Parm.CODE);
		RESULT result = RESULT.ERROR;
		if(isNotNull(code)){
			TopicDifficulty td = tdDAO.get(Integer.parseInt(code));
			if(td != null){
				td.setDeleted(true);
				result = tdDAO.upd(td);
			}
		}
		
		if(result == RESULT.SUCCESS){
			tdDAO.loadAll();	//重新加载难度缓存
			print(response, Msg.SUCCESS);
		}else{
			print(response, Msg.ERROR);
		}
	}
	
	@RequestMapping(Url.UN_SHIELD)
	public void unshield(HttpServletRequest request, HttpServletResponse response){
		String code = request.getParameter(Parm.CODE);
		RESULT result = RESULT.ERROR;
		if(isNotNull(code)){
			TopicDifficulty td = tdDAO.get(Integer.parseInt(code));
			if(td != null){
				td.setDeleted(false);
				result = tdDAO.upd(td);
			}
		}
		
		if(result == RESULT.SUCCESS){
			tdDAO.loadAll();	//重新加载难度缓存
			print(response, Msg.SUCCESS);
		}else{
			print(response, Msg.ERROR);
		}
	}
	
	@Autowired
	public void setTdDAO(TopicDifficultyDAO tdDAO) {
		this.tdDAO = tdDAO;
	}
	@Autowired
	public void setOrganDAO(OrganDAO organDAO) {
		this.organDAO = organDAO;
	}
	
}

